<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>从字符串中去除 HTML 和 PHP 标记</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.strip-tags" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">strip_tags</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">strip_tags</span> &mdash; <span class="dc-title">从字符串中去除 HTML 和 PHP 标记</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.strip-tags-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>strip_tags</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$str</code></span>
   [, <span class="methodparam"><span class="type">string</span> <code class="parameter">$allowable_tags</code></span>
  ] ) : <span class="type">string</span></div>

  <p class="para rdfs-comment">
   该函数尝试返回给定的字符串 <code class="parameter">str</code> 去除空字符、HTML 和 PHP 标记后的结果。它使用与函数 <span class="function"><a href="fgetss.html" class="function">fgetss()</a></span> 一样的机制去除标记。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.strip-tags-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">str</code></dt>

     <dd>

      <p class="para">
       输入字符串。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">allowable_tags</code></dt>

     <dd>

      <p class="para">
       使用可选的第二个参数指定不被去除的字符列表。
      </p>
      <blockquote class="note"><p><strong class="note">Note</strong>: 
       <p class="para">
        HTML 注释和 PHP 标签也会被去除。这里是硬编码处理的，所以无法通过 <code class="parameter">allowable_tags</code> 参数进行改变。
       </p>
      </p></blockquote>
      <blockquote class="note"><p><strong class="note">Note</strong>: 
       <p class="para">
        In PHP 5.3.4 and later, self-closing
        XHTML tags are ignored and only non-self-closing tags should be used in
        <code class="parameter">allowable_tags</code>. For example,
        to allow both <em>&lt;br&gt;</em> and
        <em>&lt;br/&gt;</em>, you should use:
       </p>
       <div class="informalexample">
        <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />strip_tags</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'&lt;br&gt;'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
        </div>

       </div>
      </p></blockquote>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.strip-tags-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回处理后的字符串。
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.strip-tags-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>5.3.4</td>
       <td>
        <span class="function"><strong>strip_tags()</strong></span> ignores self-closing XHTML
        tags in
        <code class="parameter">allowable_tags</code>.
       </td>
      </tr>

      <tr>
       <td>5.0.0</td>
       <td>
        <span class="function"><strong>strip_tags()</strong></span> 变为二进制安全的。
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.strip-tags-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-5965">
    <p><strong>Example #1 <span class="function"><strong>strip_tags()</strong></span> 范例</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$text&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'&lt;p&gt;Test&nbsp;paragraph.&lt;/p&gt;&lt;!--&nbsp;Comment&nbsp;--&gt;&nbsp;&lt;a&nbsp;href="#fragment"&gt;Other&nbsp;text&lt;/a&gt;'</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">strip_tags</span><span style="color: #007700">(</span><span style="color: #0000BB">$text</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;允许&nbsp;&lt;p&gt;&nbsp;和&nbsp;&lt;a&gt;<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">strip_tags</span><span style="color: #007700">(</span><span style="color: #0000BB">$text</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'&lt;p&gt;&lt;a&gt;'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Test paragraph. Other text
&lt;p&gt;Test paragraph.&lt;/p&gt; &lt;a href=&quot;#fragment&quot;&gt;Other text&lt;/a&gt;
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.strip-tags-notes">
  <h3 class="title">注释</h3>
  <div class="warning"><strong class="warning">Warning</strong>
   <p class="para">
    由于 <span class="function"><strong>strip_tags()</strong></span> 无法实际验证 HTML，不完整或者破损标签将导致更多的数据被删除。
   </p>
  </div>
  <div class="warning"><strong class="warning">Warning</strong>
   <p class="para">
    该函数不会修改 <code class="parameter">allowable_tags</code> 参数中指定的允许标记的任何属性，包括 <em>style</em> 和 <em>onmouseover</em> 属性，用户可能会在提交的内容中恶意滥用这些属性，从而展示给其他用户。
   </p>
  </div>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    输入 HTML 标签名字如果大于 1023 字节(bytes)将会被认为是无效的，无论
    <code class="parameter">allowable_tags</code> 参数是怎样的。
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.strip-tags-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="htmlspecialchars.html" class="function" rel="rdfs-seeAlso">htmlspecialchars()</a> - 将特殊字符转换为 HTML 实体</span></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>